package com.hspedu.reflection;

import java.lang.reflect.InvocationTargetException;
import java.lang.reflect.Method;

/**
 * @author 谷其昊
 * @version 1.0
 * @date 2022/3/23
 * @time 18:35
 */
public class ReflectionAccessMethod {
    public static void main(String[] args) throws ClassNotFoundException, IllegalAccessException, InstantiationException, NoSuchMethodException, InvocationTargetException {
        Class<?> bossClass = Class.forName("com.hspedu.reflection.Boss");
        Object boss = bossClass.newInstance();
        Method hi = bossClass.getMethod("hi", String.class);
        hi.invoke(boss,"hsp");

        Method say = bossClass.getDeclaredMethod("say", int.class, String.class, char.class);
        say.setAccessible(true);
        System.out.println(say.invoke(boss, 100, "hsp", 'a'));
        System.out.println(say.invoke(null, 100, "hsp", 'a'));
        Object invoke = say.invoke(null, 100, "hsp", 'a');
        System.out.println(invoke.getClass());
    }
}

class Monster {}

class Boss {//类
    public int age;
    private static String name;

    public Boss() {//构造器
    }

    public Monster m1() {
        return new Monster();
    }

    private static String say(int n, String s, char c) {//静态方法
        return n + " " + s + " " + c;
    }

    public void hi(String s) {//普通public方法
        System.out.println("hi " + s);
    }
}